﻿namespace DistrRTgen_Service
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Diagnostics;
    using System.ServiceProcess;

    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private IContainer components = null;
        private ServiceInstaller serviceInstaller1;
        private ServiceProcessInstaller serviceProcessInstaller1;

        public ProjectInstaller()
        {
            this.InitializeComponent();
            this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
            this.serviceProcessInstaller1.Username = "";
            this.serviceProcessInstaller1.Password = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.serviceProcessInstaller1 = new ServiceProcessInstaller();
            this.serviceInstaller1 = new ServiceInstaller();
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            this.serviceInstaller1.Description = "DistrRTgen Service";
            this.serviceInstaller1.DisplayName = "DistrRTgen Service";
            this.serviceInstaller1.ServiceName = "DistrRTgen Service";
            this.serviceInstaller1.StartType = ServiceStartMode.Automatic;
            base.Installers.AddRange(new Installer[] { this.serviceProcessInstaller1, this.serviceInstaller1 });
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            ServiceController controller = new ServiceController("DistrRTgen Service");
            try
            {
                controller.Start();
            }
            catch (Exception ex)
            {
                string source = "DistrRTgen Installer";
                string log = "Application";
                if (!EventLog.SourceExists(source))
                {
                    EventLog.CreateEventSource(source, log);
                }
                EventLog eLog = new EventLog();
                eLog.Source = source;
                eLog.WriteEntry("The service could not be started. Please start the service manually. Error: " + ex.Message, EventLogEntryType.Error);
            }
        }
    }
}

